package com.example.seakdreamandroid;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.sleep.Sleep;

import com.example.seakdreamandroid.firstPage.FirstPage;
import com.example.seakdreamandroid.train.Train;
import com.example.seakdreamandroid.whiteNoise.WhiteNoiseActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerService;

import java.util.ArrayList;
import java.util.List;

/**
 * Android端
 */
public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private RadioButton tab1,tab2,tab3,tab4;  //四个单选按钮
    private List<View> mViews;   //存放视图
    private Button play123;
    /*
        杨锐涵添加
     */
    private static User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initView();//初始化数据

        int id = getIntent().getIntExtra("id", 0);
        if (id == 1) {
            tab2.setChecked(true);
        }

        play123 = findViewById(R.id.play123);
        play123.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user.isMSPIsPlay()) {
                    Intent intent = new Intent();
                    intent.putExtra("audioUrl", user.getAudioUrl());
                    intent.putExtra("audioId", user.getAudioId());
                    intent.putExtra("audioName", user.getAudioName());
                    intent.putExtra("audioAuthor", user.getAudioAuthor());
                    intent.setClass(MainActivity.this, MusicStoryPlayerActivity.class);
                    startActivity(intent);
                } else if(user.isWhiteNoiseIsPlay()){
                    Intent intent = new Intent(MainActivity.this, WhiteNoisePlayerActivity.class);
                    startActivity(intent);
                } else{
                    Intent intent = new Intent(MainActivity.this, WhiteNoiseActivity.class);
                    startActivity(intent);
                }
            }
        });
    }


    private void initView() {
        //初始化控件
        mViewPager=findViewById(R.id.viewpager);
        mRadioGroup=findViewById(R.id.rg_tab);
        tab1=findViewById(R.id.rb_msg);
        tab2=findViewById(R.id.rb_people);
        tab3=findViewById(R.id.rb_find);
        tab4=findViewById(R.id.rb_me);
        /*
        杨锐涵增加
         */
        //service  杨锐涵
        user = (User) getApplication();
        Intent intent1 = new Intent(this,WhiteNoisePlayerService.class);
        startService(intent1);

        Drawable drawable_mine = getResources().getDrawable(R.drawable.mine_drawable);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_mine.setBounds(0, 0, 80, 80);
        //设置图片在文字的哪个方向
        tab4.setCompoundDrawables(null, drawable_mine, null, null);


        Drawable drawable_a = getResources().getDrawable(R.drawable.sleep_drawable);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_a.setBounds(0, 0, 80, 80);
        tab2.setCompoundDrawables(null, drawable_a, null, null);

        Drawable drawable_b = getResources().getDrawable(R.drawable.main_drawable);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_b.setBounds(0, 0, 80, 80);
        tab1.setCompoundDrawables(null, drawable_b, null, null);

        Drawable drawable_c = getResources().getDrawable(R.drawable.xunlian_drawable);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_c.setBounds(0, 0, 80, 80);
        tab3.setCompoundDrawables(null, drawable_c, null, null);


        List<Fragment> fragments = new ArrayList<>();
        FirstPage a= new FirstPage();
        Sleep b= new Sleep();
        C c= new C();
        MineActivity mineActivity = new MineActivity();
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rb_msg:
                    tab1.setChecked(true);
                    mViewPager.setCurrentItem(0, true);
                    break;
                case R.id.rb_people:
                    tab2.setChecked(true);
                    mViewPager.setCurrentItem(1, true);
                    break;
                case R.id.rb_find:
                    tab3.setChecked(true);
                    mViewPager.setCurrentItem(2, true);
                    break;
                case R.id.rb_me:
                    tab4.setChecked(true);
                    mViewPager.setCurrentItem(3, true);
                    break;

            }
        });


        fragments.add(a);
        fragments.add(b);
        fragments.add(new Train());
        fragments.add(mineActivity);

        mViewPager.setAdapter(adapter);


       /* mViews=new ArrayList<View>();//加载，添加视图
        mViews.add(LayoutInflater.from(this).inflate(R.layout.mine_activity,null));
        mViews.add(LayoutInflater.from(this).inflate(R.layout.activity_login,null));
        mViews.add(LayoutInflater.from(this).inflate(R.layout.mine_activity,null));
        mViews.add(LayoutInflater.from(this).inflate(R.layout.mine_activity,null));
        mViewPager.setAdapter(new MyViewPagerAdapter());//设置一个适配器*/
       /* //对viewpager监听，让分页和底部图标保持一起滑动
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override   //让viewpager滑动的时候，下面的图标跟着变动
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tab1.setChecked(true);
                        tab2.setChecked(false);
                        tab3.setChecked(false);
                        tab4.setChecked(false);
                        break;
                    case 1:
                        tab1.setChecked(false);
                        tab2.setChecked(true);
                        tab3.setChecked(false);
                        tab4.setChecked(false);
                        break;
                    case 2:
                        tab1.setChecked(false);
                        tab2.setChecked(false);
                        tab3.setChecked(true);
                        tab4.setChecked(false);
                        break;
                    case 3:
                        tab1.setChecked(false);
                        tab2.setChecked(false);
                        tab3.setChecked(false);
                        tab4.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });*/
    }

    //ViewPager适配器
    private class MyViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mViews.size();
        }


        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(mViews.get(position));
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(mViews.get(position));
            return mViews.get(position);
        }

    }
    /*
    杨锐涵添加
 */
    //用于实现连接服务，比较模板化，不需要详细知道内容
//    static class MyServiceConn implements ServiceConnection {
//
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            Log.e("onServiceConnected","onServiceConnected");
//            whiteNoiseControl = (WhiteNoisePlayerService.WhiteNoiseControl) service;
//
//
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    }
//    /*
//    杨锐涵添加
// */
//    //判断服务是否被解绑
//    private void unbind(boolean isUnbind){
//        //如果解绑了
//
//        if(!isUnbind){
//            whiteNoiseControl.pausePlay();//音乐暂停播放
//            user.setWhiteNoiseIsPlay(false);
//            unbindService(conn);//解绑服务
//
//
//        }
//    }


}